/**
 * @author Kevin
 *
 */

import java.io.*;

import javax.crypto.*;
import javax.crypto.spec.*;

public class JavaxDecrypt {
    //
    // Method for reading object stored in a file.
    //
    private static Object readFromFile(String filename) throws Exception {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        Object object = null;

        try {
            fis = new FileInputStream(new File(filename));
            ois = new ObjectInputStream(fis);
            object = ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                ois.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
        return object;
    }

    public static void main(String[] args) throws Exception {
        //
        // Read the previously stored SecretKey.
        //
        //SecretKey key = (SecretKey) readFromFile("secretkey.dat");

        //
        // Read the SealedObject
        //
        //SealedObject sealedObject = (SealedObject) readFromFile("sealed.dat");

        //
        // Preparing Cipher object from decryption.
        //
        //String algorithmName = sealedObject.getAlgorithm();
        try
        {
        	System.out.println("Top ");
        	//Get the 56-bit value
        	 byte[] raw = new byte[]{0x01, 0x72, 0x43, 0x3E, 0x1C, 0x7A, 0x55};
        	 byte[] keyBytes = addParity(raw);

        	 // You can check that the parity has been set properly
        	 try {
        	     boolean b = DESKeySpec.isParityAdjusted(keyBytes, 0);
        	 } catch (java.security.InvalidKeyException e) {
        	     // The DES is invalid
        	 }

        	 // Convert the bytes into a SecretKey suitable for use by Cipher
        	 SecretKey key = new SecretKeySpec(keyBytes, "DES");
        	 System.out.println(key);
        	 DesEncrypter de = new DesEncrypter(key);
        	 System.out.println("Made it this far");
        	 byte[] ciphertext = de.encrypt("HiMom");
        	 System.out.println("Encrypted");
        	 System.out.println(ciphertext);
        	 String result = de.decrypt(ciphertext);
        	 System.out.println(result);
        	/*
        	Cipher ecipher = Cipher.getInstance("DES");
        	Cipher dcipher = Cipher.getInstance("DES");
            ecipher.init(Cipher.ENCRYPT_MODE, key);
            dcipher.init(Cipher.DECRYPT_MODE, key);
            
            String text = "HiMom";//(String) sealedObject.getObject(cipher);
            System.out.println("Text = " + new CipherOu(text));
        */
        }
        catch (Exception e)
        {
        	System.out.print(e);
        }
        
    }
    
	 // Takes a 7-byte quantity and returns a valid 8-byte DES key.
	 // The input and output bytes are big-endian, where the most significant
	 // byte is in element 0.
	 public static byte[] addParity(byte[] in) {
	     byte[] result = new byte[8];
	
	     // Keeps track of the bit position in the result
	     int resultIx = 1;
	
	     // Used to keep track of the number of 1 bits in each 7-bit chunk
	     int bitCount = 0;
	
	     // Process each of the 56 bits
	     for (int i=0; i<56; i++) {
	         // Get the bit at bit position i
	         boolean bit = (in[6-i/8]&(1<<(i%8))) > 0;
	
	         // If set, set the corresponding bit in the result
	         if (bit) {
	             result[7-resultIx/8] |= (1<<(resultIx%8))&0xFF;
	             bitCount++;
	         }
	
	         // Set the parity bit after every 7 bits
	         if ((i+1) % 7 == 0) {
	             if (bitCount % 2 == 0) {
	                 // Set low-order bit (parity bit) if bit count is even
	                 result[7-resultIx/8] |= 1;
	             }
	             resultIx++;
	             bitCount = 0;
	         }
	         resultIx++;
	     }
	     return result;
	 }
}
